From bb52365b6f131b14539b4cc1cc2790fae44c9495 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Sat, 21 Jun 2014 23:51:55 -0700 Subject: [PATCH] Simplify flags --- libs/hammer.rs | 2 +- src/cargo/lib.rs | 37 +++++++++++-------------------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/libs/hammer.rs b/libs/hammer.rs index ea9e456ef..0baf235e2 160000 --- a/libs/hammer.rs +++ b/libs/hammer.rs @@ -1 +1 @@ -Subproject commit ea9e456ef5e0dbc5dc2e071f33622e9fc917a918 +Subproject commit 0baf235e24d6c3e20f3ae8368d1c0fce08138bd4 diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index ec1cd8997..bee98bdab 100644 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -23,7 +23,7 @@ use serialize::{Decoder, Encoder, Decodable, Encodable, json}; use std::io; use std::io::{stdout, stderr}; use std::io::stdio::{stdout_raw, stderr_raw}; -use hammer::{FlagDecoder, FlagConfig, UsageDecoder, HammerError}; +use hammer::{Flags, decode_args, usage}; use core::{Shell, MultiShell, ShellConfig}; use term::color::{BLACK}; @@ -63,19 +63,6 @@ pub mod ops; pub mod sources; pub mod util; -trait FlagParse : FlagConfig { - fn decode_flags(d: &mut FlagDecoder) -> Result; -} - -impl> FlagParse for T { - fn decode_flags(d: &mut FlagDecoder) -> Result { - Decodable::decode(d) - } -} - -trait RepresentsFlags : FlagParse + Decodable {} -impl> RepresentsFlags for T {} - trait RepresentsJSON : Decodable {} impl> RepresentsJSON for T {} @@ -96,13 +83,13 @@ hammer_config!(GlobalFlags |c| { }) pub fn execute_main<'a, - T: RepresentsFlags, + T: Flags, U: RepresentsJSON, V: Encodable, io::IoError>>( exec: fn(T, U, &mut MultiShell) -> CliResult>) { fn call<'a, - T: RepresentsFlags, + T: Flags, U: RepresentsJSON, V: Encodable, io::IoError>>( exec: fn(T, U, &mut MultiShell) -> CliResult>, @@ -120,12 +107,12 @@ pub fn execute_main<'a, } pub fn execute_main_without_stdin<'a, - T: RepresentsFlags, + T: Flags, V: Encodable, io::IoError>>( exec: fn(T, &mut MultiShell) -> CliResult>) { fn call<'a, - T: RepresentsFlags, + T: Flags, V: Encodable, io::IoError>>( exec: fn(T, &mut MultiShell) -> CliResult>, shell: &mut MultiShell, @@ -140,7 +127,7 @@ pub fn execute_main_without_stdin<'a, } fn process<'a, - T: RepresentsFlags, + T: Flags, V: Encodable, io::IoError>>( callback: |&[String], &mut MultiShell| -> CliResult>) { @@ -151,7 +138,7 @@ fn process<'a, let mut shell = shell(val.verbose); if val.help { - let (desc, options) = hammer::usage::(true); + let (desc, options) = usage::(true); desc.map(|d| println!("{}\n", d)); @@ -159,7 +146,7 @@ fn process<'a, print!("{}", options); - let (_, options) = hammer::usage::(false); + let (_, options) = usage::(false); print!("{}", options); } else { process_executed(callback(val.rest.as_slice(), &mut shell), &mut shell) @@ -236,16 +223,14 @@ fn args() -> Vec { std::os::args() } -fn flags_from_args(args: &[String]) -> CliResult { - let mut decoder = FlagDecoder::new::(args); - FlagParse::decode_flags(&mut decoder).map_err(|e: HammerError| { +fn flags_from_args(args: &[String]) -> CliResult { + decode_args(args).map_err(|e| { CliError::new(e.message, 1) }) } fn global_flags() -> CliResult { - let mut decoder = FlagDecoder::new::(args().tail()); - Decodable::decode(&mut decoder).map_err(|e: HammerError| { + decode_args(args().tail()).map_err(|e| { CliError::new(e.message, 1) }) } -- 2.30.2